<?php

include 'config.php';
include 'game.php';

$ws = new websocket($config['address'], $config['port']);

class websocket{
    public $game;
    public $server;

    function __construct($address, $port) {
        $this->game = new game($this);
        $this->server = new swoole_websocket_server($address, $port);

        $this->server->on('open', function (swoole_websocket_server $server, $request) {
            echo "client {$request->fd} connect\n";
        });

        $this->server->on('message', function (swoole_websocket_server $server, $frame) {
            $this->game->event($frame->data, $frame->fd, $frame->fd);
        });

        $this->server->on('close', function ($ser, $fd) {
            echo "client $fd lost\n";
            $this->game->event('{"type":"leaveRoom"}', $fd, $fd);
        });

        $this->server->start();
    }

    public function send($msg, $id){
        $this->server->push($id, $msg);
}




}

$server = new swoole_websocket_server($config['address'], $config['port']);

